﻿package Interface
{
	import Core.InterfacePage;	
	import Interface.ProfileClasses.ProfileItem;
	import Interface.ShopClasses.ShopItem;
	
	import flash.events.MouseEvent;	
	import flash.text.TextField;
	
	public class PageProfile extends InterfacePage
	{
		private var FAvatar:AvatarLoader;
		private var FDynamics:ProfileItem;
		private var FEqualizers:ProfileItem;
		private var FHeadphones:ProfileItem;
		private var FAmplifiers:ProfileItem;
		private var FVinils:ProfileItem;
		private var FInfo:TextField;
		private var FRating:TextField;
		private var FWins:TextField;
		private var FLoses:TextField;
		private var FItems:Vector.<ProfileItem>;
		
		public function PageProfile():void
		{
			FAvatar = getChildByName('Photo') as AvatarLoader;
			FItems = new Vector.<ProfileItem>;
			
			FDynamics = FindItem('DynamicsSet', 'Динамики');
			FEqualizers = FindItem('EqualizersSet', 'Еквалайзер');
			FHeadphones = FindItem('HeadphonesSet', 'Наушники');
			FAmplifiers = FindItem('AmplifiersSet', 'Усилитель');
			FVinils = FindItem('VinilsSet', 'Винил');
			
			FInfo = getChildByName('InfoText') as TextField;
			FRating = getChildByName('RatingText') as TextField;
			FWins = getChildByName('WinsText') as TextField;
			FLoses = getChildByName('LosesText') as TextField;
		}
		
		private function FindItem(AName:String, ALabel:String):ProfileItem
		{
			var vResult:ProfileItem = getChildByName(AName) as ProfileItem;			
			vResult.addEventListener(MouseEvent.MOUSE_OVER, OnItemSelect);
			vResult.addEventListener(MouseEvent.MOUSE_OUT, OnItemDeselect);
			vResult.Label = ALabel;
			FItems.push(vResult);
			return vResult;	
		}
		
		private function OnItemSelect(e:MouseEvent):void
		{
			with(e.target as ProfileItem)
				FInfo.text = Label + '. ' + 'Бонус: X' + Bonus.toString();
		}
		
		private function OnItemDeselect(e:MouseEvent):void
		{
			FInfo.text = 'Наведите курсор мыши на обуродование, чтобы узнать его характеристики';
		}
		
		public function set PhotoURL(APath:String):void
		{
			FAvatar.Photo = APath;
		}
		
		public function set Equipment(AEquipment:Vector.<int>):void
		{
			for (var i in AEquipment)
			{
				if(AEquipment[i] > -1)
				{
					AddItem(AEquipment[i]);
				}
				else
				{
					FItems[i].visible = false;
				}
			}
		}
		
		public function ChangeItem(AItem:ShopItem):void
		{
			FItems[AItem.Type].visible = true;
			FItems[AItem.Type].gotoAndStop(AItem.ProfileGraphic);
			FItems[AItem.Type].Bonus = AItem.Bonus;			
			FGame.User.UpdateItem(AItem);
			FGame.UpdateProfile();
		}
		
		private function AddItem(AGlobalID:int):void
		{
			var vProfileItem:ProfileItem;
			var vShopItem:ShopItem = null;
			
			vShopItem = FGame.Shop.GetItemById(AGlobalID);
			if(vShopItem)
			{
				FItems[vShopItem.Type].gotoAndStop(vShopItem.ProfileGraphic);		
				FItems[vShopItem.Type].Bonus = vShopItem.Bonus;
			}
		}
		
		public function get Bonus():Number
		{
			var vResult:Number = new Number();
			for(var i in FItems)
				vResult += FItems[i].Bonus;			
			return vResult;
		}
		
		public function ComputeBonus(AEquipment:Vector.<int>):Number
		{
			var vResult:Number = new Number();			
			for (var i in AEquipment)
			{
				if(AEquipment[i] > -1)
					vResult += FGame.Shop.GetItemById(AEquipment[i]).Bonus;
			}
			return vResult;
		}
		
		public function Update():void
		{
			FRating.text = FGame.User.Rating;
			FWins.text = FGame.User.Wins;
			FLoses.text = FGame.User.Loses;			
		}
	}	
}
